package com.remeberword;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class WordPairDetailActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private TextView textView;
    private String englishTra, chineseTra;
    private MyDatabaseHelper dbHelper;
    private SQLiteDatabase db;
    private EditText englishText;
    private EditText chineseText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_pair_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        textView = (TextView) findViewById(R.id.freq);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.show_word:
                        startActivity(new Intent(WordPairDetailActivity.this, MainActivity.class));
                        break;
                    case R.id.freq_manager:
                        startActivity(new Intent(WordPairDetailActivity.this, WordManageActivity.class));
                        break;
                    case R.id.word_add:
                        startActivity(new Intent(WordPairDetailActivity.this, AddWordActivity.class));
                        break;
                    case R.id.backup:
                        startActivity(new Intent(WordPairDetailActivity.this, BackupRestoreActivity.class));
                        break;
                }
                mDrawerLayout.closeDrawers();
                return true;
            }
        });
        String[] wordPiar = getIntent().getStringExtra("wordPiar").split("&");
        englishTra = wordPiar[0];
        chineseTra = wordPiar[1];

        dbHelper = new MyDatabaseHelper(this, "WordPair.db", null);
        db = dbHelper.getWritableDatabase();

        ImageButton decreaseButton = (ImageButton) findViewById(R.id.decrease);
        decreaseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Integer.parseInt(textView.getText().toString()) <= 1)
                    return;
                String sql = "update wordPair set frequency = frequency - 1 where english = ? and chinese = ?";
                db.execSQL(sql, new String[]{englishTra, chineseTra});
                textView.setText(Integer.parseInt(textView.getText().toString()) - 1 + "");
            }
        });

        ImageButton increaseButton = (ImageButton) findViewById(R.id.increase);
        increaseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String sql = "update wordPair set frequency = frequency + 1 where english = ? and chinese = ?";
                db.execSQL(sql, new String[]{englishTra, chineseTra});
                textView.setText(Integer.parseInt(textView.getText().toString()) + 1 + "");
            }
        });


        englishText = (EditText) findViewById(R.id.input_english);
        chineseText = (EditText) findViewById(R.id.input_chinese);
        Button modifyButton = (Button) findViewById(R.id.submit);
        modifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String english = englishText.getText().toString().trim();
                String chinese = chineseText.getText().toString().trim();
                if (english.length() == 0 || chinese.length() == 0) {
                    Toast.makeText(WordPairDetailActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                Cursor cursor = db.rawQuery("select count(*) from wordPair where english = ?", new String[]{english});
                if (cursor.moveToFirst()) {
                    if (! english.equals(englishTra) && cursor.getInt(0) > 0) {
                        AlertDialog.Builder dialog = new AlertDialog.Builder(WordPairDetailActivity.this);
                        dialog.setMessage("已存在该单词");
                        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                            }
                        });
                        dialog.show();
                    } else {
                        String sql = "update wordPair set english = ?, chinese = ? where english = ?";
                        db.execSQL(sql, new String[]{english, chinese, englishTra});
                        Toast.makeText(WordPairDetailActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                        englishTra = english;
                        chineseTra = chinese;
                        englishText.requestFocus();
                    }
                }
                cursor.close();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Cursor cursor = db.rawQuery("select frequency from wordPair where english = ? and chinese = ?", new String[]{englishTra, chineseTra});
        if (cursor.moveToFirst()) {
            int frequency = cursor.getInt(cursor.getColumnIndex("frequency"));
            textView.setText(frequency + "");
        }
        cursor.close();
        englishText.setText(englishTra);
        chineseText.setText(chineseTra);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.delete:
                AlertDialog.Builder dialog = new AlertDialog.Builder(WordPairDetailActivity.this);
                dialog.setMessage("确定要删除该单词吗？");
                dialog.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String sql = "delete from wordPair where english = ?";
                        db.execSQL(sql, new String[]{englishTra});
                        finish();
                    }
                });
                dialog.setNegativeButton("点错了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                dialog.show();
                break;
            default:
        }
        return true;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }
}